package de.fhaugsburg.games.boardgames;

import java.util.List;
import java.util.Random;
import java.util.Vector;
import de.fhaugsburg.games.boardgames.exceptions.*;


public class RandomPool<T> {

	private Random random = new Random();
	private List<T> collection = new Vector<T>();

	/**
	 * Puts something in the pool
	 * @param o
	 */
	public void put(T o) {
		collection.add(o);
	}
	
	/**
	 * Returns random something from the pool, and then removes it
	 * @return
	 */
	public T take() {
		
		if (collection.isEmpty()) {
			throw new SackException("Sack ist leer!");
		}
		
		int rnd = random.nextInt(collection.size());		
		T element = collection.get(rnd);
		collection.remove(rnd);
		return element;
	}
	
	/**
	 * Returns boolean if RandomPool is empty
	 * @return
	 */
	public boolean empty() {
		return collection.isEmpty();
	}
	
	/**
	 * Returns RandomPool content as String
	 */
	public String toString() {
		
		int i =  0;
		String sTmp = ""; 
		
		for (i=0; i<collection.size();i++) {
			sTmp += collection.get(i);
		}
		return sTmp;
	}
	
	/**
	 * Clears the RandomPool
	 */
	public void reset() {
		this.collection.clear();
	}
	
	
}
